package com.starblink.android.common.utils

import android.Manifest
import android.app.Activity
import android.content.Intent
import com.blankj.utilcode.util.PermissionUtils
import com.king.zxing.CameraScan
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.extension.navigationTo
import com.starblink.basic.log.SKLogger
import com.starblink.basic.route.RoutePage

object ScanUtil {
    private const val REQUEST_CODE_SCAN:Int=999
    fun startScan(mContext:Activity,ifAutoJump:Boolean=true){
        PermissionUtils.permission(Manifest.permission.CAMERA)
            .callback { isAllGranted, granted, _, _ ->
                if (isAllGranted) {
                    // 获得授权
                    mContext.navigationTo(RoutePage.Web.PAGE_GLOBAL_SCAN, REQUEST_CODE_SCAN){
                        withBoolean(RoutePage.Web.ARG_AUTO_JUMP,ifAutoJump)
                    }
                } else {
                    toast("lack of camera permissions")
                }
            }.request()
    }
    fun getScanResult(requestCode: Int, resultCode: Int, data: Intent?):String?{
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_SCAN && data != null) {
            val result = CameraScan.parseScanResult(data)
            SKLogger.d("ScanUtil","扫描的二维码地址--- $result")
            return result
        }
        return null
    }
}